package gogogo.liwushuo.activity;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import gogogo.liwushuo.R;
import gogogo.liwushuo.adapter.LoadXlsqAdapter;
import gogogo.liwushuo.app.IApp;
import gogogo.liwushuo.bean.XlsqBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.util.VolleyUtils;

/**
 * Created by Puck on 2016/7/20.
 */
public class XuanLiShenQiActivity extends Activity implements Toolbar.OnMenuItemClickListener {
    @InjectView(R.id.xlsq_toolbar)
    Toolbar xlsqToolbar;
    @InjectView(R.id.tv_xlsq_all)
    TextView tvXlsqAll;
    @InjectView(R.id.iv_xlsq_all)
    ImageView ivXlsqAll;
    @InjectView(R.id.tv_xlsq_Occasion)
    TextView tvXlsqOccasion;
    @InjectView(R.id.iv_xlsq_Occasion)
    ImageView ivXlsqOccasion;
    @InjectView(R.id.tv_xlsq_Personallity)
    TextView tvXlsqPersonallity;
    @InjectView(R.id.iv_xlsq_Personallity)
    ImageView ivXlsqPersonallity;
    @InjectView(R.id.tv_xlsq_Price)
    TextView tvXlsqPrice;
    @InjectView(R.id.iv_xlsq_Price)
    ImageView ivXlsqPrice;
    @InjectView(R.id.rv_xlsq)
    RecyclerView rvXlsq;
    @InjectView(R.id.xlsq_toolbar_menu)
    ImageView xlsqToolbarMenu;
    @InjectView(R.id.llayout)
    LinearLayout llayout;
    @InjectView(R.id.back)
    ImageView back;

    private PopupWindow mPopupWindow;
    private List<XlsqBean.DataBean.ItemsBean> mDatas;
    private LoadXlsqAdapter mAdapter;
    private GridLayoutManager mLayoutManger;
    private boolean arrayGreyState = false;
    private boolean tagAll;
    private boolean tagOccasion;
    private boolean tagPrice;
    private boolean tagToolbar;
    private boolean tagPersonal;
    //    private List<String> mPopupPriceDatas;
//    private List<Integer> mPopupFavirtDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuanlishenqi);
        ButterKnife.inject(this);
        xlsqToolbar.setOnMenuItemClickListener(this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mDatas = new ArrayList<>();
        rvXlsq = (RecyclerView) findViewById(R.id.rv_xlsq);

        mAdapter = new LoadXlsqAdapter(this, mDatas);
        mLayoutManger = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
        rvXlsq.setLayoutManager(mLayoutManger);
        rvXlsq.setAdapter(mAdapter);
        loadData();
        rvXlsq.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int itemCount = mLayoutManger.getItemCount();
                int lastPos = mLayoutManger.findLastVisibleItemPosition();
                if ((lastPos + 1 == itemCount) && (newState == RecyclerView.SCROLL_STATE_IDLE)) {
                    loadData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    private void loadData() {
        VolleyUtils.newInstance(this).newGsonRequest(Request.Method.GET, Resource.URL_XLSQ_MAIN_PAGE,
                new Response.Listener<XlsqBean>() {
                    @Override
                    public void onResponse(XlsqBean response) {
                        mDatas.addAll(response.getData().getItems());
//                        for(int i=0;i<mDatas.size();i++){
//                            mPopupPriceDatas.add(mDatas.get(i).getPrice());
//                            mPopupFavirtDatas.add(mDatas.get(i).getFavorites_count());
//                        }
                        mAdapter.setList(mDatas);
                        mAdapter.notifyDataSetChanged();
                        Log.i("mDatas", "mDatas2--->" + mDatas);
                    }
                }, null, XlsqBean.class);
    }

    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }

    @OnClick({R.id.tv_xlsq_all, R.id.xlsq_toolbar_menu, R.id.iv_xlsq_all, R.id.tv_xlsq_Occasion, R.id.iv_xlsq_Occasion, R.id.tv_xlsq_Personallity, R.id.iv_xlsq_Personallity, R.id.tv_xlsq_Price, R.id.iv_xlsq_Price})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.xlsq_toolbar_menu:
                if (!tagToolbar) {
                    showToolbarPopUpWindow();
                } else {
                    mPopupWindow.dismiss();
                }
                tagToolbar = !tagToolbar;
                break;
            case R.id.tv_xlsq_all:
                if (!tagAll) {
                    showAllPopUpWindow();
                } else {
                    mPopupWindow.dismiss();
                }
                tagAll = !tagAll;
                break;

            case R.id.iv_xlsq_all:
                if (!tagAll) {
                    showAllPopUpWindow();
                } else {
                    mPopupWindow.dismiss();
                }
                tagAll = !tagAll;
                break;
            case R.id.tv_xlsq_Occasion:
                if (!tagOccasion) {
                    showOccasionPopUpWindow();
                } else {
                    mPopupWindow.dismiss();
                }
                tagOccasion = !tagOccasion;
                break;
            case R.id.iv_xlsq_Occasion:
                if (!tagOccasion) {
                    showOccasionPopUpWindow();
                } else {
                    mPopupWindow.dismiss();
                }
                tagOccasion = !tagOccasion;
                break;
            case R.id.tv_xlsq_Personallity:
                if (!tagPersonal) {
                    showPersonalityAllPopUpWindow();
                } else {
                    mPopupWindow.dismiss();
                }

                tagPersonal = !tagPersonal;
                break;
            case R.id.iv_xlsq_Personallity:
                if (!tagPersonal) {
                    showPersonalityAllPopUpWindow();
                } else {
                    mPopupWindow.dismiss();
                }

                tagPersonal = !tagPersonal;
                break;
            case R.id.tv_xlsq_Price:
                if (!tagPrice) {
                    showPriceAllPopUpWindow();
                } else {
                    mPopupWindow.dismiss();
                }

                tagPrice = !tagPrice;
                break;
            case R.id.iv_xlsq_Price:
                if (!tagPrice) {
                    showPriceAllPopUpWindow();
                } else {
                    mPopupWindow.dismiss();
                }

                tagPrice = !tagPrice;
                break;
        }
    }

    private void showAllPopUpWindow() {
        ivXlsqAll.setImageResource(R.mipmap.arrow_grey_up);
        View contentView = LayoutInflater.from(this).inflate(R.layout.xlsq_all_popup_window, null);
        mPopupWindow = new PopupWindow(contentView, IApp.mScreenWidth, IApp.mScreenHeight / 3);
        mPopupWindow.setContentView(contentView);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.showAsDropDown(llayout, 0, 30);
    }

    private void showOccasionPopUpWindow() {
        ivXlsqOccasion.setImageResource(R.mipmap.arrow_grey_up);
        View contentView = LayoutInflater.from(this).inflate(R.layout.xlsq_occasion_popup_window, null);
        mPopupWindow = new PopupWindow(contentView, IApp.mScreenWidth, IApp.mScreenHeight / 3);
        mPopupWindow.setContentView(contentView);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.showAsDropDown(ivXlsqOccasion, 0, 30);
    }

    private void showPersonalityAllPopUpWindow() {

        ivXlsqPersonallity.setImageResource(R.mipmap.arrow_grey_up);
        View contentView = LayoutInflater.from(this).inflate(R.layout.xlsq_personality_popup_window, null);
        mPopupWindow = new PopupWindow(contentView, IApp.mScreenWidth, IApp.mScreenHeight / 3);
        mPopupWindow.setContentView(contentView);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.showAsDropDown(ivXlsqPersonallity, 0, 30);
    }

    private void showPriceAllPopUpWindow() {

        ivXlsqPrice.setImageResource(R.mipmap.arrow_grey_up);
        View contentView = LayoutInflater.from(this).inflate(R.layout.xlsq_price_popup_window, null);
        mPopupWindow = new PopupWindow(contentView, IApp.mScreenWidth, IApp.mScreenHeight / 3);
        mPopupWindow.setContentView(contentView);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.showAsDropDown(ivXlsqPrice, 0, 30);
    }

    private void showToolbarPopUpWindow() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.xlsq_toolbar_menu_popup_window, null);
        mPopupWindow = new PopupWindow(contentView, IApp.mScreenWidth, IApp.mScreenHeight / 3);
        mPopupWindow.setContentView(contentView);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.showAsDropDown(xlsqToolbarMenu, 0, 30);
    }


}
